/**
 * 还原被转义的Html特殊字符
 * @param {string} str
 * @returns {string}
 */
function restoreHtml(str = '') {
    if (str.length > 0) {
        str = str.replace(/&lt;/g, "<");
        str = str.replace(/&gt;/g, ">");
        str = str.replace(/&quot;/g, '"');
    }
    return str;
}

/**
 * 转义特殊字符
 * @param {string} str
 * @returns {string}
 */
function kickHtml(str) {
    if (str.length > 0) {
        str = str.replace(/</g, "&lt;");
        str = str.replace(/>/g, "&gt;");
        str = str.replace(/"/g, '&quot;');
    }
    return str;
}

/**
 * 显示换行
 * @param {string} str
 * @returns {string}
 */
function showText(str) {
    let string = kickHtml(str);
    string = string.replace(/\r\n/g, "<br>");
    string = string.replace(/\n/g, "<br>");
    string = string.replace(/\s/g, "&nbsp;");
    return string;
}

/**
 * 将br标签替换为js换行
 * @param {string} str
 * @returns {string}
 */
function backText(str) {
    str = str.replace(/<br>/g, "\n");
    str = str.replace(/<br\/>/g, "\n");
    str = str.replace(/&nbsp;/g, " ");
    return str;
}


/**
 * 部分编辑器生成的代码中，图片或表格等代码中有width:xxpx的样式，该方法用于将宽度大于375的部分替换为100%
 * @param str
 * @returns {string}
 */
function clearWidth(str) {
    str = str.replace(/width:(\s+|)([\d\.]+)px/g, function (word, word1, value, c) {
            console.log('word', word,);
            console.log('value', value);
            return value > 375 ? 'width:100%' : 'width:' + value + 'px';
        }
    );
    str = str.replace(/width:100%;height:(\s|)([\d\.]+)px/g, function (word, value) {
            return 'width:100%;';
        }
    );
    return str
}


export {
    restoreHtml,
    kickHtml,
    showText,
    backText,
    clearWidth

}
